using System;
using System.Collections.Generic;
using System.Data;

using Elev.Core.DAL;

namespace Elev.Core
{
    public class GradesFactory
    {
        public List<Grade> GetGrades(int StudentID, int PeriodID)
        {
            GradeInternal g = new GradeInternal();
            DataTable dt = g.GetGrades(StudentID, PeriodID);
            List<Grade> list = new List<Grade>();

            if (dt.Rows.Count > 0)
                foreach (DataRow dr in dt.Rows)
                    list.Add(new Grade(dr));

            return list;
        }

        public Grade GetGrade(int StudentID, int ExamID)
        {
            GradeInternal g = new GradeInternal();
            DataTable dt = g.GetGrade(StudentID, ExamID);

            if (dt.Rows.Count > 1)
                throw new ArgumentException(string.Format("There is more than one grade for the student '{0}' and the exam '{1}'",
                    StudentID, ExamID));

            if (dt.Rows.Count == 0)
            {
                // There is no grade yet for this couple Student / Exam, return then an empty Grade Instance
                return new Grade(ExamID, StudentID);
            }

            return new Grade(dt.Rows[0]);
        }
    }
}
